﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class ccprovvacacionctservController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /ccprovvacacionctserv/

        public ActionResult Index()
        {
            return View();
        }
        //
        // GET: /ccprovvacacionctserv/indexconid
        public ActionResult Indexconid(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            //string area = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.CCPROVVACACIONCTSERVs
                .Where(ccpv => ccpv.IdProvVacacion == id);
            /*if (!String.IsNullOrEmpty(area))
            {
                query = query.Where(a => (a.Nombre).ToUpper().Contains(area.ToUpper()));
            }*/
            var result = (from ccpv in query.OrderByDescending(ccpv => ccpv.IdCCProvVacacionCTServ).Skip(start).Take(limit).ToList()
                          join py in entidades.PROYECTOes on ccpv.IdProyecto equals py.IdProyecto

                          join cc in entidades.CCOSTOes on ccpv.IdCCosto equals cc.IdCCosto into ccl
                          from x in ccl.DefaultIfEmpty()

                          join pc in entidades.PLANCONTABLEs on ccpv.IdPlan equals pc.IdPlan into pcl
                          from y in pcl.DefaultIfEmpty()

                          select new
                          {
                              Id = ccpv.IdCCProvVacacionCTServ,
                              IdProvVacacion = ccpv.IdProvVacacion,
                              CodCCosto = (x == null ? String.Empty : x.Codigo),
                              IdProyecto = py.IdProyecto,
                              Proyecto = py.Nombre,
                              NomCCosto = (x == null ? String.Empty : NomCC(x.IdCCosto)),
                              Plan = (y == null ? String.Empty : y.Nombre),
                              Monto = ccpv.valor,
                              Porcentaje = ccpv.porcentaje,
                              Estado = ccpv.estado
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        //ccprovvacacionctserv/indexconiddos
        public string NomCC(int id) {
            var query = entidades.CCOSTOes.Where(cc => cc.IdCCosto == id)
                .Join(entidades.ELEMENTOGASTOes, cc => cc.IdElemGasto, eg => eg.IdElemGasto, (cc, eg) => new { cc.IdCCosto,eg.NomElemGasto });

            return query.Single(cc => cc.IdCCosto == id).NomElemGasto;
        }
        public ActionResult Indexconiddos(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);
            //string area = Convert.ToString(Request.QueryString["query"]);
            var query = entidades.CCPROVVACACIONCTSERVs
                .Where(ccpv => ccpv.IdProvCTServ == id);
            /*if (!String.IsNullOrEmpty(area))
            {
                query = query.Where(a => (a.Nombre).ToUpper().Contains(area.ToUpper()));
            }*/
            var result = (from ccpv in query.OrderByDescending(ccpv => ccpv.IdCCProvVacacionCTServ).Skip(start).Take(limit).ToList()
                          join py in entidades.PROYECTOes on ccpv.IdProyecto equals py.IdProyecto

                          join cc in entidades.CCOSTOes on ccpv.IdCCosto equals cc.IdCCosto into ccl
                          from x in ccl.DefaultIfEmpty()

                          join pc in entidades.PLANCONTABLEs on ccpv.IdPlan equals pc.IdPlan into pcl
                          from y in pcl.DefaultIfEmpty()

                          select new
                          {
                              Id = ccpv.IdCCProvVacacionCTServ,
                              IdProvVacacion = ccpv.IdProvVacacion,
                              CodCCosto = (x == null ? String.Empty : x.Codigo),
                              IdProyecto = py.IdProyecto,
                              Proyecto = py.Nombre,
                              NomCCosto = (x == null ? String.Empty : NomCC(x.IdCCosto)),
                              Plan = (y == null ? String.Empty : y.Nombre),
                              Monto = ccpv.valor,
                              Porcentaje = ccpv.porcentaje,
                              Estado = ccpv.estado
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /ccprovvacacionctserv/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ccprovvacacionctserv/Create

        public ActionResult Create()
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO PROVISIONADO", Value = "false" });
            siono.Add(new SelectListItem { Text = "PROVISIONADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text");

            return View();
        } 

        //
        // POST: /ccprovvacacionctserv/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, CCPROVVACACIONCTSERV CCPROVVACACIONCTSERV)
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO PROVISIONADO", Value = "false" });
            siono.Add(new SelectListItem { Text = "PROVISIONADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text");

            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    var cccprovvacacion = new CCPROVVACACIONCTSERV();
                    TryUpdateModel(cccprovvacacion, collection.ToValueProvider()); ;
                    entidades.AddToCCPROVVACACIONCTSERVs(cccprovvacacion);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ccprovvacacionctserv/Edit/5
 
        public ActionResult Edit(int id)
        {
            var eccprovvacacion = entidades.CCPROVVACACIONCTSERVs.Single(eccpv => eccpv.IdCCProvVacacionCTServ == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO PROVISIONADO", Value = "false" });
            siono.Add(new SelectListItem { Text = "PROVISIONADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text", eccprovvacacion.estado);

            return View(eccprovvacacion);
        }

        //
        // POST: /ccprovvacacionctserv/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection,CCPROVVACACIONCTSERV CCPROVVACACIONCTSERV)
        {
            var eccprovvacacion = entidades.CCPROVVACACIONCTSERVs.Single(eccpv => eccpv.IdCCProvVacacionCTServ == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO PROVISIONADO", Value = "false" });
            siono.Add(new SelectListItem { Text = "PROVISIONADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text", eccprovvacacion.estado);

            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    //var ccontrato = new CONTRATOADENDAPERSONAL();
                    TryUpdateModel(eccprovvacacion);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View(eccprovvacacion);
            }
            catch
            {
                return View(eccprovvacacion);
            }
        }

        //
        // GET: /ccprovvacacionctserv/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ccprovvacacionctserv/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
